package games.ticTacToe_;

import java.util.Scanner;

public class Demo {
	private static Scanner sc = new Scanner(System.in);

	public static void main(String[] args) {
		System.out.println("Enter size for the board:");
		System.out.print("Rows:");
		int rows = checkValidNumber();
		System.out.print("Columns:");
		int columns = checkValidNumber();

		System.out.print("Choose how many similar fields are needed to win:");
		int n = checkValidNumber();

		SeaChess game = new SeaChess(rows, columns, n);
		game.printBoard();
		do {
			System.out
					.println("\nPlayer " + game.getPlayer() + ". Mark field:");
			int[] point=checkMarkPoint();
			game.mark(point[0], point[1]);
		} while (game.hasWinner() == false && game.isDraw() == false);

		if (game.isDraw()) {
			System.out.println("Draw!");
		} else {
			System.out.println("Winner is player " + game.getPlayer());
		}
		System.out.println("GAME OVER!");

	}

	static private int[] checkMarkPoint() {
		System.out.print("row: ");
		String row = sc.nextLine();
		System.out.print("column: ");
		String columns = sc.nextLine();
		int[] point = new int[2];
		
		row=row.trim();
		columns=columns.trim();
		try {
			point[0] = Integer.parseInt(row);
		} catch (NumberFormatException nfe) {
			System.out.println("Invalid value. Try Again");
			return checkMarkPoint();
		}
		try {
			point[1] = Integer.parseInt(columns);
		} catch (NumberFormatException nfe) {
			System.out.println("Invalid value. Try Again");
			return checkMarkPoint();
		}
		return point;
	}

	static private int checkValidNumber() {
		int n = 0;
		String str = sc.nextLine();
		str=str.trim();
		try {
			n = Integer.parseInt(str);
		} catch (NumberFormatException nfe) {
			System.out.println("Invalid value. Try Again");
			return checkValidNumber();
		}
		return n;

	}
}
